package com.example.myapp_1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.example.myapp_1.util.MediaUtil;
import com.example.myapp_1.worker.PeriodicWorker;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private static final String PeriodicWorkerTag = "periodic_worker_1";

    @BindView(R.id.btn_1) Button btn_1;
    @BindView(R.id.btn_2) Button btn_2;
    @BindView(R.id.btn_3) Button btn_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_1, R.id.btn_2, R.id.btn_3})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_1:
                changeVolume();
                break;
            case R.id.btn_2:
                add_WorkRequest();
                break;
            case R.id.btn_3:
                remove_WorkRequest();
                break;
        }
    }

    // 设置 来电铃声 为系统最大音量
    private void changeVolume() {
        MediaUtil.changeVolume(MainActivity.this);
    }

    // 添加定时任务
    private void add_WorkRequest() {
        // 避免重复添加
        WorkManager.getInstance(this).cancelAllWorkByTag(PeriodicWorkerTag);

        PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(PeriodicWorker.class, 30, TimeUnit.MINUTES)
                .addTag(PeriodicWorkerTag)
                .build();
        WorkManager.getInstance(this).enqueue(request);
    }

    // 移除定时任务
    private void remove_WorkRequest() {
        WorkManager.getInstance(this).cancelAllWorkByTag(PeriodicWorkerTag);
    }

}
